package com.mooc.kafka.consumer;

import com.mooc.kafka.admin.AdminSimple;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author StarsOfFuture_xYang
 * @version 1.0
 * @date 2021-04-11 10:37 下午
 * @information kafka-study - com.mooc.kafka.consumer
 **/
public class FxMyThreadConsumer {

    public static void main(String[] args) throws InterruptedException {
        ConsumerThread consumerThread = new ConsumerThread();
        consumerThread.executor(10);
        Thread.sleep(30);
        consumerThread.shutdown();
    }


    static class ConsumerThread {
        private KafkaConsumer<String, String> kafkaConsumer;
        private ExecutorService executorService;

        public ConsumerThread() {
            Properties properties = new Properties();
            properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "1.116.23.168:9092");
            properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "test");
            properties.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
            properties.setProperty(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000");
            properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
            properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
            kafkaConsumer = new KafkaConsumer<>(properties);
            kafkaConsumer.subscribe(Arrays.asList(AdminSimple.TOPIC_NAME));
        }

        public void executor(int maxSize) {
            executorService = new ThreadPoolExecutor(maxSize, maxSize, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100), new ThreadPoolExecutor.CallerRunsPolicy());
            ConsumerRecords<String, String> poll = kafkaConsumer.poll(Duration.ofMillis(1000));
            while (true) {
                if (poll.isEmpty()) {
                    System.err.println("无可消费消息!");
                    break;
                }
                for (ConsumerRecord<String, String> consumerRecord : poll) {
                    executorService.submit(new RecordRunnable(consumerRecord));
                }
            }
        }

        public void shutdown() {
            if (kafkaConsumer != null) {
                kafkaConsumer.close();
            }
            if (executorService != null) {
                executorService.shutdown();
            }
            try {
                if (!executorService.awaitTermination(10, TimeUnit.SECONDS)) {
                    System.err.println("超时!");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    }

    static class RecordRunnable implements Runnable {
        private ConsumerRecord consumerRecord;

        public RecordRunnable(ConsumerRecord consumerRecord) {
            this.consumerRecord = consumerRecord;
        }

        @Override
        public void run() {
            System.out.printf("partition=%d ,offset=%d,key=%s ,value=%s ", consumerRecord.partition(), consumerRecord.offset(), consumerRecord.key(), consumerRecord.value());
        }
    }
}
